#tag Window
Begin Window AlbumImportWin
   BackColor       =   &hFFFFFF
   Backdrop        =   ""
   CloseButton     =   True
   Composite       =   False
   Frame           =   0
   FullScreen      =   False
   HasBackColor    =   False
   Height          =   440
   ImplicitInstance=   True
   LiveResize      =   False
   MacProcID       =   0
   MaxHeight       =   32000
   MaximizeButton  =   False
   MaxWidth        =   32000
   MenuBar         =   ""
   MenuBarVisible  =   True
   MinHeight       =   64
   MinimizeButton  =   False
   MinWidth        =   64
   Placement       =   0
   Resizeable      =   False
   Title           =   "Import Album"
   Visible         =   True
   Width           =   6.0e+2
   Begin Listbox songList
      AutoDeactivate  =   True
      AutoHideScrollbars=   True
      Bold            =   ""
      Border          =   True
      ColumnCount     =   3
      ColumnsResizable=   ""
      ColumnWidths    =   "40%,30%,30%"
      DataField       =   ""
      DataSource      =   ""
      DefaultRowHeight=   22
      Enabled         =   True
      EnableDrag      =   ""
      EnableDragReorder=   ""
      GridLinesHorizontal=   0
      GridLinesVertical=   0
      HasHeading      =   True
      HeadingIndex    =   -1
      Height          =   243
      HelpTag         =   ""
      Hierarchical    =   ""
      Index           =   -2147483648
      InitialParent   =   ""
      InitialValue    =   "    Track	Artist	File"
      Italic          =   ""
      Left            =   20
      LockBottom      =   ""
      LockedInPosition=   False
      LockLeft        =   True
      LockRight       =   ""
      LockTop         =   True
      RequiresSelection=   ""
      Scope           =   0
      ScrollbarHorizontal=   ""
      ScrollBarVertical=   True
      SelectionType   =   0
      TabIndex        =   0
      TabPanelIndex   =   0
      TabStop         =   True
      TextFont        =   "System"
      TextSize        =   10
      TextUnit        =   0
      Top             =   14
      Underline       =   ""
      UseFocusRing    =   True
      Visible         =   True
      Width           =   560
      _ScrollWidth    =   -1
   End
   Begin StaticText sourceST
      AutoDeactivate  =   True
      Bold            =   ""
      DataField       =   ""
      DataSource      =   ""
      Enabled         =   True
      Height          =   20
      HelpTag         =   ""
      Index           =   -2147483648
      InitialParent   =   ""
      Italic          =   ""
      Left            =   20
      LockBottom      =   ""
      LockedInPosition=   False
      LockLeft        =   True
      LockRight       =   ""
      LockTop         =   True
      Multiline       =   ""
      Scope           =   0
      TabIndex        =   1
      TabPanelIndex   =   0
      TabStop         =   True
      Text            =   "Source folder:"
      TextAlign       =   0
      TextColor       =   &h000000
      TextFont        =   "System"
      TextSize        =   0
      TextUnit        =   0
      Top             =   350
      Underline       =   ""
      Visible         =   True
      Width           =   108
   End
   Begin TextField TextField1
      AcceptTabs      =   ""
      Alignment       =   0
      AutoDeactivate  =   True
      BackColor       =   &hFFFFFF
      Bold            =   ""
      Border          =   True
      CueText         =   ""
      DataField       =   ""
      DataSource      =   ""
      Enabled         =   True
      Format          =   ""
      Height          =   22
      HelpTag         =   ""
      Index           =   -2147483648
      Italic          =   ""
      Left            =   140
      LimitText       =   0
      LockBottom      =   ""
      LockedInPosition=   False
      LockLeft        =   True
      LockRight       =   ""
      LockTop         =   True
      Mask            =   ""
      Password        =   ""
      ReadOnly        =   ""
      Scope           =   0
      TabIndex        =   2
      TabPanelIndex   =   0
      TabStop         =   True
      Text            =   ""
      TextColor       =   &h000000
      TextFont        =   "System"
      TextSize        =   0
      TextUnit        =   0
      Top             =   348
      Underline       =   ""
      UseFocusRing    =   True
      Visible         =   True
      Width           =   211
   End
   Begin BevelButton BrowseFolder
      AcceptFocus     =   False
      AutoDeactivate  =   True
      BackColor       =   "&c000000"
      Bevel           =   0
      Bold            =   False
      ButtonType      =   0
      Caption         =   "..."
      CaptionAlign    =   3
      CaptionDelta    =   0
      CaptionPlacement=   1
      Enabled         =   True
      HasBackColor    =   False
      HasMenu         =   0
      Height          =   22
      HelpTag         =   ""
      Icon            =   ""
      IconAlign       =   0
      IconDX          =   0
      IconDY          =   0
      Index           =   -2147483648
      InitialParent   =   ""
      Italic          =   False
      Left            =   363
      LockBottom      =   ""
      LockedInPosition=   False
      LockLeft        =   True
      LockRight       =   ""
      LockTop         =   True
      MenuValue       =   0
      Scope           =   0
      TabIndex        =   3
      TabPanelIndex   =   0
      TabStop         =   True
      TextColor       =   "&c000000"
      TextFont        =   "System"
      TextSize        =   ""
      TextUnit        =   0
      Top             =   348
      Underline       =   False
      Value           =   False
      Visible         =   True
      Width           =   30
   End
   Begin StaticText albnameST
      AutoDeactivate  =   True
      Bold            =   ""
      DataField       =   ""
      DataSource      =   ""
      Enabled         =   True
      Height          =   20
      HelpTag         =   ""
      Index           =   -2147483648
      InitialParent   =   ""
      Italic          =   ""
      Left            =   20
      LockBottom      =   ""
      LockedInPosition=   False
      LockLeft        =   True
      LockRight       =   ""
      LockTop         =   True
      Multiline       =   ""
      Scope           =   0
      TabIndex        =   4
      TabPanelIndex   =   0
      TabStop         =   True
      Text            =   "Album name:"
      TextAlign       =   0
      TextColor       =   &h000000
      TextFont        =   "System"
      TextSize        =   0
      TextUnit        =   0
      Top             =   374
      Underline       =   ""
      Visible         =   True
      Width           =   108
   End
   Begin TextField TextField2
      AcceptTabs      =   ""
      Alignment       =   0
      AutoDeactivate  =   True
      BackColor       =   &hFFFFFF
      Bold            =   ""
      Border          =   True
      CueText         =   ""
      DataField       =   ""
      DataSource      =   ""
      Enabled         =   True
      Format          =   ""
      Height          =   22
      HelpTag         =   ""
      Index           =   -2147483648
      Italic          =   ""
      Left            =   140
      LimitText       =   0
      LockBottom      =   ""
      LockedInPosition=   False
      LockLeft        =   True
      LockRight       =   ""
      LockTop         =   True
      Mask            =   ""
      Password        =   ""
      ReadOnly        =   ""
      Scope           =   0
      TabIndex        =   5
      TabPanelIndex   =   0
      TabStop         =   True
      Text            =   ""
      TextColor       =   &h000000
      TextFont        =   "System"
      TextSize        =   0
      TextUnit        =   0
      Top             =   373
      Underline       =   ""
      UseFocusRing    =   True
      Visible         =   True
      Width           =   253
   End
   Begin PushButton Import
      AutoDeactivate  =   True
      Bold            =   ""
      Cancel          =   ""
      Caption         =   "Import"
      Default         =   ""
      Enabled         =   True
      Height          =   22
      HelpTag         =   ""
      Index           =   -2147483648
      InitialParent   =   ""
      Italic          =   ""
      Left            =   500
      LockBottom      =   ""
      LockedInPosition=   False
      LockLeft        =   True
      LockRight       =   ""
      LockTop         =   True
      Scope           =   0
      TabIndex        =   6
      TabPanelIndex   =   0
      TabStop         =   True
      TextFont        =   "System"
      TextSize        =   0
      TextUnit        =   0
      Top             =   400
      Underline       =   ""
      Visible         =   True
      Width           =   80
   End
   Begin PushButton Cancel
      AutoDeactivate  =   True
      Bold            =   ""
      Cancel          =   True
      Caption         =   "Cancel"
      Default         =   ""
      Enabled         =   True
      Height          =   22
      HelpTag         =   ""
      Index           =   -2147483648
      InitialParent   =   ""
      Italic          =   ""
      Left            =   414
      LockBottom      =   ""
      LockedInPosition=   False
      LockLeft        =   True
      LockRight       =   ""
      LockTop         =   True
      Scope           =   0
      TabIndex        =   7
      TabPanelIndex   =   0
      TabStop         =   True
      TextFont        =   "System"
      TextSize        =   0
      TextUnit        =   0
      Top             =   400
      Underline       =   ""
      Visible         =   True
      Width           =   80
   End
   Begin PopupMenu catalogMenu
      AutoDeactivate  =   True
      Bold            =   ""
      DataField       =   ""
      DataSource      =   ""
      Enabled         =   True
      Height          =   20
      HelpTag         =   ""
      Index           =   -2147483648
      InitialParent   =   ""
      InitialValue    =   ""
      Italic          =   ""
      Left            =   140
      ListIndex       =   0
      LockBottom      =   ""
      LockedInPosition=   False
      LockLeft        =   True
      LockRight       =   ""
      LockTop         =   True
      Scope           =   0
      TabIndex        =   8
      TabPanelIndex   =   0
      TabStop         =   True
      TextFont        =   "System"
      TextSize        =   0
      TextUnit        =   0
      Top             =   400
      Underline       =   ""
      Visible         =   True
      Width           =   253
   End
   Begin StaticText catalogST
      AutoDeactivate  =   True
      Bold            =   ""
      DataField       =   ""
      DataSource      =   ""
      Enabled         =   True
      Height          =   20
      HelpTag         =   ""
      Index           =   -2147483648
      InitialParent   =   ""
      Italic          =   ""
      Left            =   20
      LockBottom      =   ""
      LockedInPosition=   False
      LockLeft        =   True
      LockRight       =   ""
      LockTop         =   True
      Multiline       =   ""
      Scope           =   0
      TabIndex        =   9
      TabPanelIndex   =   0
      TabStop         =   True
      Text            =   "Catalog:"
      TextAlign       =   0
      TextColor       =   &h000000
      TextFont        =   "System"
      TextSize        =   0
      TextUnit        =   0
      Top             =   400
      Underline       =   ""
      Visible         =   True
      Width           =   108
   End
   Begin ProgressBar prBar
      AutoDeactivate  =   True
      Enabled         =   True
      Height          =   20
      HelpTag         =   ""
      Index           =   -2147483648
      InitialParent   =   ""
      Left            =   20
      LockBottom      =   ""
      LockedInPosition=   False
      LockLeft        =   True
      LockRight       =   ""
      LockTop         =   True
      Maximum         =   100
      Scope           =   0
      TabIndex        =   10
      TabPanelIndex   =   0
      TabStop         =   True
      Top             =   268
      Value           =   0
      Visible         =   True
      Width           =   560
   End
   Begin CheckBox parentByArtist
      AutoDeactivate  =   True
      Bold            =   ""
      Caption         =   "Parent folder is named by the Artist"
      DataField       =   ""
      DataSource      =   ""
      Enabled         =   True
      Height          =   20
      HelpTag         =   ""
      Index           =   -2147483648
      InitialParent   =   ""
      Italic          =   ""
      Left            =   20
      LockBottom      =   ""
      LockedInPosition=   False
      LockLeft        =   True
      LockRight       =   ""
      LockTop         =   True
      Scope           =   0
      State           =   0
      TabIndex        =   10
      TabPanelIndex   =   0
      TabStop         =   True
      TextFont        =   "System"
      TextSize        =   0
      TextUnit        =   0
      Top             =   316
      Underline       =   ""
      Value           =   False
      Visible         =   True
      Width           =   373
   End
   Begin CheckBox removeTrackNumbers
      AutoDeactivate  =   True
      Bold            =   ""
      Caption         =   "Remove track numbers"
      DataField       =   ""
      DataSource      =   ""
      Enabled         =   True
      Height          =   20
      HelpTag         =   ""
      Index           =   -2147483648
      InitialParent   =   ""
      Italic          =   ""
      Left            =   20
      LockBottom      =   ""
      LockedInPosition=   False
      LockLeft        =   True
      LockRight       =   ""
      LockTop         =   True
      Scope           =   0
      State           =   0
      TabIndex        =   11
      TabPanelIndex   =   0
      TabStop         =   True
      TextFont        =   "System"
      TextSize        =   0
      TextUnit        =   0
      Top             =   293
      Underline       =   ""
      Value           =   False
      Visible         =   True
      Width           =   373
   End
   Begin StaticText progText
      AutoDeactivate  =   True
      Bold            =   ""
      DataField       =   ""
      DataSource      =   ""
      Enabled         =   True
      Height          =   20
      HelpTag         =   ""
      Index           =   -2147483648
      InitialParent   =   ""
      Italic          =   ""
      Left            =   449
      LockBottom      =   ""
      LockedInPosition=   False
      LockLeft        =   True
      LockRight       =   ""
      LockTop         =   True
      Multiline       =   ""
      Scope           =   0
      TabIndex        =   12
      TabPanelIndex   =   0
      TabStop         =   True
      Text            =   ""
      TextAlign       =   0
      TextColor       =   &h000000
      TextFont        =   "System"
      TextSize        =   0
      TextUnit        =   0
      Top             =   325
      Underline       =   ""
      Visible         =   True
      Width           =   100
   End
End
#tag EndWindow

#tag WindowCode
	#tag Event
		Sub Open()
		  me.left=SCREEN(0).WIDTH/2-me.width/2
		  me.top=100
		  
		  app.lang.LoadCaptions(self)
		End Sub
	#tag EndEvent


	#tag Method, Flags = &h0
		Sub LoadTracks()
		  dim s as string
		  
		  songList.DeleteAllRows
		  
		  redim SongDataArray(-1)
		  
		  if SourceFolder<>nil then
		    If SourceFolder.Directory then
		      
		      for i as integer=1 to SourceFolder.Count
		        
		        if not SourceFolder.Item(i).Directory then
		          
		          if not (realbasic.left(SourceFolder.Item(i).Name,1)=".") then
		            
		            s=NthField(SourceFolder.Item(i).Name,".",1)
		            
		            if removeTrackNumbers.value then
		              songlist.addrow(right(s,(len(s)-3)))
		            else
		              songList.addrow(s)
		            end
		            
		            songList.Cell(songList.lastindex,2)=SourceFolder.Item(i).Name
		            
		            songList.RowPicture(songList.lastindex)=Close20x20
		            
		            songList.ColumnType(0)=songList.TypeEditableTextField
		            songList.ColumnType(1)=songList.TypeEditableTextField
		            songList.ColumnType(2)=songList.TypeDefault
		            
		            dim m as new MusicDataObject
		            
		            if parentByArtist.value then
		              songList.Cell(songList.lastindex,1)=Sourcefolder.Parent.Name
		              m.PERFORMER=Sourcefolder.Parent.Name
		            end
		            
		            m.HIRESFILE=New Folderitem(Sourcefolder.Item(i))
		            m.CATEGORYID=catalogMenu.listindex
		            m.ALBUMNAME=TextField2.text
		            m.TRACKNAME=NthField(SourceFolder.Item(i).Name,".",1)
		            m.EXTLOCATION=SourceFolder.Item(i).AbsolutePath
		            
		            songList.rowTag(songlist.lastindex)=m
		            
		            SongDataArray.append(m)
		            
		          end
		        end
		        
		      next
		    end
		  end
		End Sub
	#tag EndMethod

	#tag Method, Flags = &h0
		Sub RefreshView()
		  for i as integer=0 to songlist.ListCount-1
		    
		    dim m as MusicDataObject
		    m=songlist.rowtag(i)
		    
		    if m.Populated then
		      songlist.RowPicture(i)=Ok_20x20
		    else
		      songlist.RowPicture(i)=Close20x20
		    end
		    
		  next
		End Sub
	#tag EndMethod

	#tag Method, Flags = &h0
		Function Transfer() As Boolean
		  if SourceFolder<>nil then
		    
		    dim f as folderitem
		    dim id as integer
		    
		    prBar.maximum=songList.ListCount
		    
		    for i as integer=0 to songList.listcount-1
		      
		      prBar.value=i+1
		      
		      do
		      loop until SongDataArray(i).Upload(false)
		      
		    next
		    
		  end
		  
		  return true
		End Function
	#tag EndMethod


	#tag Property, Flags = &h0
		ClickedColumn As Integer
	#tag EndProperty

	#tag Property, Flags = &h0
		ClickedRow As Integer
	#tag EndProperty

	#tag Property, Flags = &h0
		SongDataArray(-1) As MusicDataObject
	#tag EndProperty

	#tag Property, Flags = &h0
		SourceFolder As Folderitem
	#tag EndProperty


#tag EndWindowCode

#tag Events songList
	#tag Event
		Function MouseDown(x As Integer, y As Integer) As Boolean
		  ClickedColumn=me.ColumnFromXY(x,y)
		  ClickedRow=me.RowFromXY(x,y)
		End Function
	#tag EndEvent
	#tag Event
		Sub DoubleClick()
		  if ClickedColumn=0 then
		    
		    if len(me.cell(ClickedRow,1))>0 then
		      SongDataArray(ClickedRow).PERFORMER=me.cell(ClickedRow,1)
		    end
		    
		    SongDataArray(ClickedRow).CATEGORYID=catalogMenu.listindex
		    
		    RMMetaWin.show
		    RMMetaWin.MySong=SongDataArray(ClickedRow)
		    RMMetaWin.SetView
		    RMMetaWin.SaveBackToImportWin=true
		    
		  end
		End Sub
	#tag EndEvent
#tag EndEvents
#tag Events BrowseFolder
	#tag Event
		Sub Action()
		  Dim dlg as New SelectFolderDialog
		  Dim f,f1 as FolderItem
		  
		  if len(app.mprefs.GetString("LastImportedFolder"))>0 then
		    f1=Getfolderitem(app.mprefs.GetString("LastImportedFolder"))
		    if f1<>nil then
		      
		    else
		      f1=SpecialFolder.Music
		    end
		  else
		    f1=SpecialFolder.Music
		  end
		  
		  if not f1.exists then f1=SpecialFolder.Documents
		  
		  dlg.ActionButtonCaption="Select"
		  dlg.Title="Select Folder"
		  dlg.PromptText="Source folder to import"
		  dlg.InitialDirectory=f1
		  
		  f=dlg.ShowModal()
		  
		  if f <> Nil then
		    
		    app.mprefs.PutString("LastImportedFolder",f.absolutepath)
		    
		    TextField1.text=f.Absolutepath
		    TextField2.text=Uppercase(f.Name)
		    
		    SourceFolder=New Folderitem(f)
		    
		    LoadTracks
		    
		  end if
		End Sub
	#tag EndEvent
#tag EndEvents
#tag Events Import
	#tag Event
		Sub Action()
		  do
		  loop until Transfer
		  
		  close
		End Sub
	#tag EndEvent
#tag EndEvents
#tag Events Cancel
	#tag Event
		Sub Action()
		  close
		End Sub
	#tag EndEvent
#tag EndEvents
#tag Events catalogMenu
	#tag Event
		Sub Open()
		  if mydb<>nil then
		    
		    dim rs as recordset
		    rs=mydb.SQLSelect("SELECT * FROM "+app.mprefs.getstring("dbschema")+".catalog ORDER BY rowid")
		    if rs<>nil then
		      while not rs.eof
		        me.addrow DefineEncoding(rs.field("value"),Encodings.UTF8)
		        me.rowtag(me.listcount-1)=rs.field("rowid").Integervalue
		        rs.movenext
		      wend
		    end
		  end
		End Sub
	#tag EndEvent
#tag EndEvents
